package com.ls.kdlss.ui.Login;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gm.common.utils.ActivityUtil;
import com.gm.common.utils.ToastUtil;
import com.gm.lib.views.titlebar.AbTitleBar;
import com.ls.kdlss.R;
import com.ls.kdlss.entity.LogingEntity;
import com.ls.kdlss.util.HttpStorage;
import com.ls.kdlss.util.IsPhoneAndNull;
import com.ls.kdlss.util.UserInfo;
import com.wx.android.common.util.JsonUtils;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/10/26.
 */

public class LogActivity extends AppCompatActivity implements View.OnClickListener {


    @Bind(R.id.title_bar)
    AbTitleBar titleBar;
    @Bind(R.id.tv_forget)
    TextView tvForget;
    @Bind(R.id.bt_log)
    Button btLog;
    @Bind(R.id.ed_phone)
    EditText edPhone;
    @Bind(R.id.ed_password)
    EditText edPassword;
    private String TAG = "LogActivity";
    private OkHttpClient client;
    public static void launch(Context context) {
        ActivityUtil.startActivity(context, LogActivity.class);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        ButterKnife.bind(this);
        titleBar.setLeftVisible();
        titleBar.setTitleText("登陆");
    }


    /**
     * 传入1代表是忘记密码
     */
    @OnClick({R.id.bt_log, R.id.tv_forget})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_log:
                okNoreference(HttpStorage.logUrls(edPhone.getText().toString(), edPassword.getText().toString()));
                break;
            case R.id.tv_forget:
                RegisterActivity.launch(LogActivity.this, "1");
                break;
        }
    }


    public void okNoreference(String url) {

            if (!IsPhoneAndNull.isPhoneAndCod(LogActivity.this, edPhone.getText().toString(), IsPhoneAndNull.PHONE)) {
                return;
            }

        client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Log.e(TAG, "进入了方法");
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
            }
            @Override
            public void onResponse(okhttp3.Call call, final Response response) throws IOException {
                  String  json=  response.body().string();
                  final LogingEntity loging= JsonUtils.jsonToObject(json,LogingEntity.class);
                     runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (loging.getStatus().getSucceed().equals("1")){
                            UserInfo.userid=loging.getData().getUser_id();
                            SetInfomationActivity.lanuch(LogActivity.this);
                        }else {
                            ToastUtil.show(loging.getData().getMessage());
                        }
                    }
                });
            }
        });
    }
}
